﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Lib.Utilities;
using System.Data;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wdt_ma
{
    [OperationContract]

    public string FDM_NAM_LKE()
    {
        try
        {
            DataTable b_dt = dm_dt.FDM_NAM_LKE();
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]

    public string FDM_NAM_NH(string ma, string ten)
    {
        try
        {
            string id = dm_dt.FDM_NAM_NH(LibConvert.ObjectToDouble(ma), ten);
            return id + "#" + LibTable.TableToJson(dm_dt.FDM_NAM_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]

    public string FDM_NAM_XOA(string ma)
    {
        try
        {
            dm_dt.FDM_NAM_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDM_NAM_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //HE DT
    [OperationContract]

    public string FDT_HE_DT_LKE()
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_HE_DT_LKE();
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]

    public string FDT_HE_DT_NH(string ma, string ten)
    {
        try
        {
            string id = dm_dt.FDT_HE_DT_NH(LibConvert.ObjectToDouble(ma), ten);
            return id + "#" + LibTable.TableToJson(dm_dt.FDT_HE_DT_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]

    public string FDT_HE_DT_XOA(string ma)
    {
        try
        {
            dm_dt.FDT_HE_DT_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDT_HE_DT_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //KHOA
    [OperationContract]

    public string FDT_KHOA_LKE(string ht_dt)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_KHOA_LKE(ht_dt);
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]

    public string FDT_KHOA_NH(string ht_dt, string ma, string ten, string gchu)
    {
        try
        {
            string id = dm_dt.FDT_KHOA_NH(LibConvert.ObjectToDouble(ht_dt), LibConvert.ObjectToDouble(ma), ten, gchu);
            return id + "#" + LibTable.TableToJson(dm_dt.FDT_KHOA_LKE(ht_dt));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]

    public string FDT_KHOA_XOA(string ma, string ht_dt)
    {
        try
        {
            dm_dt.FDT_KHOA_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDT_KHOA_LKE(ht_dt));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //BOMON
    [OperationContract]

    public string FDT_BO_MON_LKE()
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_BO_MON_LKE();
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]

    public string FDT_BO_MON_NH(string ma, string ten)
    {
        try
        {
            string id = dm_dt.FDT_BO_MON_NH(LibConvert.ObjectToDouble(ma), ten);
            return id + "#" + LibTable.TableToJson(dm_dt.FDT_BO_MON_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]

    public string FDT_BO_MON_XOA(string ma)
    {
        try
        {
            dm_dt.FDT_BO_MON_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDT_BO_MON_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    // LOP
    [OperationContract]
    public string FDT_LOP_LKE(string khoa)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_LOP_LKE(LibConvert.ObjectToDouble(khoa));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_LOP_PC_DAY_LKE(string nam_id)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_LOP_PC_DAY_LKE(LibConvert.ObjectToDouble(nam_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_LOP_NH(string ma, string khoa_id, string ten, string nam_dt)
    {
        try
        {
            string id = dm_dt.FDT_LOP_NH(LibConvert.ObjectToDouble(ma), LibConvert.ObjectToDouble(khoa_id), ten, nam_dt);
            return id + "#" + LibTable.TableToJson(dm_dt.FDT_LOP_LKE(LibConvert.ObjectToDouble(khoa_id)));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_LOP_XOA(string ma, string khoa_id)
    {
        try
        {
            dm_dt.FDT_LOP_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDT_LOP_LKE(LibConvert.ObjectToDouble(khoa_id)));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    // MON HOC
    [OperationContract]
    public string FDT_MON_LKE(string bm)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_MON_LKE(LibConvert.ObjectToDouble(bm));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_MON_NH(string ma, string bm, string ten)
    {
        try
        {
            string id = dm_dt.FDT_MON_NH(LibConvert.ObjectToDouble(ma), LibConvert.ObjectToDouble(bm), ten);
            return id + "#" + LibTable.TableToJson(dm_dt.FDT_MON_LKE(LibConvert.ObjectToDouble(bm)));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_MON_XOA(string ma, string bm)
    {
        try
        {
            dm_dt.FDT_MON_XOA(LibConvert.ObjectToDouble(ma));
            return LibTable.TableToJson(dm_dt.FDT_MON_LKE(LibConvert.ObjectToDouble(bm)));
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //GIANG DUONG
    [OperationContract]
    public string FDT_GIANG_DUONG_LKE()
    {
        try
        {
            return LibTable.TableToJson(dm_dt.FDT_GIANG_DUONG_LKE());
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    //PC_GIANGDAY
    [OperationContract]
    public string FDT_PC_GIANGDAY_LKE(string hk, string nam, string lop)
    {
        try
        {

            DataTable b_dt = dm_dt.FDT_PC_GIANGDAY_LKE(LibConvert.ObjectToDouble(nam), hk, LibConvert.ObjectToDouble(lop));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_SV_VHOA_LKE(object nam_id, object lop_vh)
    {
        try
        {

            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToDouble(lop_vh) }, "PDT_DS_SV_HOC_VH");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //Lớp học văn hóa theo năm
    [OperationContract]
    public string Fs_MON_VH_LKE(object nam_id)
    {
        try
        {
            DataTable b_dt = dm_dt.Fdt_LOP_VH_LKE(Convert.ToInt64(nam_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_PC_GIANGDAY_NH(string phong_id, string ns_id, string hk, string nam, string lop, string b_ten_lop, string[] mon, string[] tc, string[] loai, string[] hso)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _se = new Lib.Helper.SessionHelper();
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "phong_id", "ns_id", "hk", "nam", "lop", "ten_lop", "mon", "tc", "loai", "hso" });
            for (int i = 0; i < mon.Length; i++)
            {
                DataRow _row = b_dt.NewRow();
                _row["nsd"] = _se.nsd;
                _row["phong_id"] = phong_id;
                _row["ns_id"] = ns_id;
                _row["hk"] = hk;
                _row["nam"] = nam;
                _row["lop"] = lop;
                _row["ten_lop"] = b_ten_lop;
                _row["mon"] = mon[i];
                _row["tc"] = (tc[i] == "" ? "-1" : tc[i]);
                _row["loai"] = loai[i];
                _row["hso"] = hso[i];

                b_dt.Rows.Add(_row);
            }
            dm_dt.FDT_PC_GIANGDAY_NH(b_dt);
            return FDT_PC_GIANGDAY_LKE(hk, nam, lop);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_PC_GIANGDAY_XOA(string ns_id, string hk, string nam, string lop, double mon)
    {
        try
        {
            dm_dt.FDT_PC_GIANGDAY_XOA(LibConvert.ObjectToDouble(ns_id), hk, LibConvert.ObjectToDouble(nam), LibConvert.ObjectToDouble(lop),
                LibConvert.ObjectToDouble(mon));
            return FDT_PC_GIANGDAY_LKE(hk, nam, lop);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    //TKB
    [OperationContract]
    public string FDT_TKB_LKE(string hk, string nam, string lop)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_TKB_LKE(LibConvert.ObjectToDouble(nam), hk, LibConvert.ObjectToDouble(lop));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_TKB_NH(string[] thu, string lop, string[] ns_id, string nam, string hk, string[] mon, string[] gd_id, string[] tg_hoc)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _se = new Lib.Helper.SessionHelper();
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "thu", "lop", "ns_id", "nam", "hk", "mon", "gd_id", "tg_hoc" });
            for (int i = 0; i < mon.Length; i++)
            {
                DataRow _row = b_dt.NewRow();
                _row["nsd"] = _se.nsd;
                _row["thu"] = thu[i];
                _row["lop"] = lop;
                _row["ns_id"] = ns_id[i];
                _row["nam"] = nam;
                _row["hk"] = hk;
                _row["mon"] = mon[i];
                _row["gd_id"] = gd_id[i];
                _row["tg_hoc"] = tg_hoc[i];

                b_dt.Rows.Add(_row);
            }
            dm_dt.FDT_TKB_NH(b_dt);
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_TKB_XOA(string thu, string lop, string ns_id, string nam_id, string hk, string mon, string gd_id)
    {
        try
        {
            dm_dt.FDT_TKB_XOA(thu, LibConvert.ObjectToDouble(lop), LibConvert.ObjectToDouble(ns_id), LibConvert.ObjectToDouble(nam_id), hk,
                LibConvert.ObjectToDouble(mon), LibConvert.ObjectToDouble(gd_id));
            return FDT_TKB_LKE(hk, nam_id, lop);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    [OperationContract]
    public string FDT_GIANGVIEN_LKE_MON(string lop, string ns_id, string nam_id, string hk)
    {
        try
        {
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { nam_id, hk, lop, ns_id }, "PTKB_GIANGVIEN_LKE_MON");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_GIANGVIEN_PC_LKE(string hk, string nam_id, string lop)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { hk, nam_id, lop }, "PTKB_GIANGVIEN_PC_LKE");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_LOP_PC_LKE(string nam_id, object hk_id)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = dm_dt.FDT_PC_DAY_LOP_LKE(LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToString(hk_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string Fs_PC_DIEM_LOP_LKE(string nam_id, string hk_id)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = dm_dt.FDT_PC_DIEM_LOP_LKE(LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToString(hk_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    [OperationContract]
    public string FDT_MON_PC_MON_LKE(string hk, string nam_id, string lop)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { hk, nam_id, lop, _nsd.id_nsd }, "P_PC_MON_LKE");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //LIỆT KÊ MÔN
    [OperationContract]
    public string FDT_MONHOC_PC_LKE(string nam_id, string lop, string hk)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { _nsd.nsd, nam_id, lop, hk }, "PTKB_MON_HOC_LKE");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //lke điểm
    [OperationContract]
    public string FDT_DIEM_LKE(string lop, string mon_id, string nam, string hk, string loai_kt, string b_loai)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_DIEM_TIM(LibConvert.ObjectToDouble(lop), LibConvert.ObjectToDouble(mon_id), LibConvert.ObjectToDouble(nam), hk, LibConvert.ObjectToDouble(loai_kt), b_loai);
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //lke điểm
    [OperationContract]
    public string FDT_DIEM_LKE_EXCEL(string lop, string mon_id, string nam, string hk, string loai_kt, string b_loai, string ten_lop, string mon)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_DIEM_TIM(LibConvert.ObjectToDouble(lop), LibConvert.ObjectToDouble(mon_id), LibConvert.ObjectToDouble(nam), hk, LibConvert.ObjectToDouble(loai_kt), b_loai);
            b_dt.TableName = "B";
            DataTable b_dt_tso = new DataTable("B1");
            DataSet b_ds = new DataSet();
            LibTable.AddNewColumns(ref b_dt_tso, new string[] { "HE", "LOP", "KHOA", "MON" });
            if (LibTable.isNullOrEmtyTable(b_dt))
                throw new Exception("loi:Không có số liệu:loi");
            DataRow _row = b_dt.Rows[0];
            if (b_loai == "RL")
                mon = "Điểm rèn luyện";
            LibTable.AddNewRows(ref b_dt_tso, new object[] { LibConvert.ObjectToString(_row["HT_DT"]), LibConvert.ObjectToString(_row["LOP"]), LibConvert.ObjectToString(_row["KHOA"]), mon });
            b_ds.Tables.Add(b_dt);
            b_ds.Tables.Add(b_dt_tso);

            string xml = "phieu_diem_vh";
            string b_out = "~/Outputs/";
            Lib.Export.Excel.ExportToExcel("~/Templates/", ref b_out, xml, b_ds);

            string[] url = b_out.Split('/');
            return url[url.Length - 1];
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //Nhập điểm
    [OperationContract]
    public string FDT_DIEM_NH(object loai_diem, object loai_kt, object[] id_dk, object[] diem)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "loai", "loai_diem", "id_dk", "diem" });
            for (int i = 0; i < id_dk.Length; i++)
            {
                DataRow _row = b_dt.NewRow();
                _row["nsd"] = _nsd.nsd;
                _row["loai"] = loai_diem;
                _row["loai_diem"] = loai_kt;
                _row["id_dk"] = id_dk[i];
                _row["diem"] = diem[i];
                b_dt.Rows.Add(_row);
            }

            //nhaapj
            Lib.Data.DataSql.ExeCuteProcedure(b_dt, "PDT_DIEM_NH");
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //Nhập điểm import
    [OperationContract]
    public string FDT_DIEM_IMPORT_NH(object loai_diem, object nam_id, object mon_id, object hk_id, object lop_id,  object loai_kt)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt_diem =(DataTable) System.Web.HttpContext.Current.Session["diem"];
            if (b_dt_diem == null)
                throw new Exception("loi:Đứt kết nối. Đăng nhập lại:loi");
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "loai", "nam", "mon", "hk", "lop", "ma_sv", "gv_id", "loai_diem", "diem" });
            for( int k=8; k< b_dt_diem.Rows.Count; k++)
            {
                if (!string.IsNullOrEmpty(LibConvert.ObjectToString(b_dt_diem.Rows[k][4])))
                {
                    DataRow _row = b_dt.NewRow();
                    _row["nsd"] = _nsd.nsd;
                    _row["loai"] = loai_diem;
                    _row["nam"] = nam_id;
                    _row["mon"] = mon_id;
                    _row["hk"] = hk_id;
                    _row["hk"] = lop_id;
                    _row["ma_sv"] = LibConvert.ObjectToString(b_dt_diem.Rows[k][1]);
                    _row["gv_id"] = _nsd.id_nsd;
                    _row["loai_diem"] = loai_kt;
                    _row["diem"] = LibConvert.ObjectToString(b_dt_diem.Rows[k][4]);
                    b_dt.Rows.Add(_row);
                    b_dt.AcceptChanges();
                }
            }

            //nhaapj
            Lib.Data.DataSql.ExeCuteProcedure(b_dt, "PDT_DIEM_IMPORT_NH");
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    //LIỆT KÊ KHÓA SỔ
    [OperationContract]
    public string FDT_KHOA_SO_LKE(string nam, string hk)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_KHOA_SO_LKE(LibConvert.ObjectToDouble(nam), hk);
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //KHÓA SỔ
    [OperationContract]
    public string FDT_KHOA_SO_NH(object nam, object hoc_ky, object[] lop, object[] b_chon)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "nam", "hoc_ky", "lop", "chon" });
            for (int i = 0; i < lop.Length; i++)
            {
                DataRow _row = b_dt.NewRow();
                _row["nsd"] = _nsd.nsd;
                _row["nam"] = nam;
                _row["hoc_ky"] = hoc_ky;
                _row["lop"] = lop[i];
                _row["chon"] = b_chon[i];
                b_dt.Rows.Add(_row);
            }

            //nhaapj
            Lib.Data.DataSql.ExeCuteProcedure(b_dt, "PDT_KHOA_SO");
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //LIỆT KÊ XEM ĐIỂM CUỐI NĂM
    [OperationContract]
    public string FDT_DIEM_CN_LKE(object nam_id, object lop_id, object hk_id)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { _nsd.nsd, nam_id, lop_id, hk_id }, "PDT_DIEM_CUOI_NAM_tungns");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //excel
    [OperationContract]
    public string FDT_DIEM_CN_LKE_EXCEL(object nam_id, object lop_id, string he, string ten_lop, string b_ten)
    {
        try
        {
            string b_out = string.Empty;
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { _nsd.nsd, nam_id, lop_id }, "PDT_DIEM_CUOI_NAM");
            DataSet b_ds = new DataSet();
            b_dt.TableName = "B";
            DataTable b_dt_tso = new DataTable("B1");
            LibTable.AddNewColumns(ref b_dt_tso, new string[] { "HE", "LOP", "TEN" });
            LibTable.AddNewRows(ref b_dt_tso, new object[] { he, ten_lop, b_ten });
            b_ds.Tables.Add(b_dt);
            b_ds.Tables.Add(b_dt_tso);

            string xml = "diem_tb_nam";
            b_out = "~/Outputs/";
            Lib.Export.Excel.ExportToExcel("~/Templates/", ref b_out, xml, b_ds);

            string[] url = b_out.Split('/');
            return url[url.Length - 1];
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //Đăng ký năm học mới
    [OperationContract]
    public string FDT_DK_NAM_MOI_NH(object nam_id)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();

            Lib.Data.DataSql.ExeCuteProcedure(new object[] { _nsd.nsd, nam_id }, "PDT_HOC_DAU_NAM");
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }


    //lke monn tn
    [OperationContract]
    public string FDT_MON_TN_LKE(object nam_id)
    {
        try
        {
            DataTable b_dt = dm_dt.FDT_MON_TN(LibConvert.ObjectToDouble(nam_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //NHẬP MÔN TỐT NGHIỆP
    [OperationContract]
    public string FDT_MON_TN_NH(object nam_id, object he_dt, object mon, object hso)
    {
        try
        {
            dm_dt.F_MON_TN_NH(LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToDouble(he_dt), LibConvert.ObjectToDouble(mon), LibConvert.ObjectToInt(hso));
            return FDT_MON_TN_LKE(nam_id);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //Xóa môn tn
    [OperationContract]
    public string FDT_MON_TN_XOA(object nam_id, string mon_id)
    {
        try
        {
            dm_dt.F_MON_TN_XOA(LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToDouble(mon_id));
            return FDT_MON_TN_LKE(nam_id);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //điểm học kỳ
    //LIỆT KÊ XEM ĐIỂM CUỐI NĂM
    [OperationContract]
    public string FDT_DIEM_HK_LKE(object nam_id, object lop_id, object hk_id)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { _nsd.nsd, nam_id, hk_id, lop_id }, "PDT_DIEM_CUOI_KY");
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    //excel
    [OperationContract]
    public string FDT_DIEM_HK_LKE_EXCEL(object nam_id, object lop_id, object hk_id, string he, string ten_lop, string b_ten)
    {
        try
        {
            string b_out = string.Empty;
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataTable b_dt = Lib.Data.DataSql.GetData(new object[] { _nsd.nsd, nam_id, hk_id, lop_id }, "PDT_DIEM_CUOI_KY");
            DataSet b_ds = new DataSet();
            b_dt.TableName = "B";
            DataTable b_dt_tso = new DataTable("B1");
            LibTable.AddNewColumns(ref b_dt_tso, new string[] { "HE", "LOP", "TEN" });
            LibTable.AddNewRows(ref b_dt_tso, new object[] { he, ten_lop, b_ten });
            b_ds.Tables.Add(b_dt);
            b_ds.Tables.Add(b_dt_tso);

            string xml = "diem_tb_nam";
            b_out = "~/Outputs/";
            Lib.Export.Excel.ExportToExcel("~/Templates/", ref b_out, xml, b_ds);

            string[] url = b_out.Split('/');
            return url[url.Length - 1];
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }
    [OperationContract]
    public string FDT_DIEM_HK_CT_LKE(object nam_id, object lop_id, object hk_id)
    {
        try
        {
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            DataSet b_ds = Lib.Data.DataSql.GetMutilData(new object[] { _nsd.nsd, nam_id, lop_id, hk_id }, "PDT_DIEM_CUOI_KY_CT");
            if (b_ds.Tables.Count == 1)
                return "";
            DataTable b_dt = b_ds.Tables[0];
            DataTable b_dt_ten = b_ds.Tables[1];
            StringBuilder str = new StringBuilder();

            str.Append("<table style=\" border-collapse: collapse;\">");
            str.Append("<tr style=\" background: #d8cfcf;\">");
            str.Append("<td style=\"width: 100px; text-align: center; border: #808080 1px solid; padding:3px;\">");
            str.Append("Mã SV");
            str.Append("</td>");

            str.Append("<td style=\"width: 190px; text-align: center; border: #808080 1px solid; padding:3px;\">");
            str.Append("Tên SV");
            str.Append("</td>");

            for (int j = 3; j < b_dt.Columns.Count; j++)
            {
                DataRow[] _row = b_dt_ten.Select("ID='" + b_dt.Columns[j].ColumnName + "'");
                if (_row.Count() > 0)
                {
                    str.Append("<td style=\"width: 100px; text-align: center; border: #808080 1px solid; padding:3px;\">");
                    str.Append(_row[0]["TEN"].ToString());
                    str.Append("</td>");
                }
            }
            str.Append("</tr>");

            for (int i = 0; i < b_dt.Rows.Count; i++)
            {
                str.Append("<tr>");
                for (int j = 1; j < b_dt.Columns.Count; j++)
                {
                    if (j == 2)
                    {
                        str.Append("<td style=\"width: 190px; text-align: center; border: #FFFFFF 1px solid; padding:3px;\">");
                        str.Append(b_dt.Rows[i][j].ToString());
                    }
                    else
                    {
                        str.Append("<td style=\"width: 100px; text-align: center; border: #FFFFFF 1px solid; padding:3px;\">");
                        str.Append(b_dt.Rows[i][j].ToString());
                    }
                    str.Append("</td>");
                }
                str.Append("</tr>");
            }
            str.Append("</table>");
            return str.ToString();
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //đăng ký lịch học
    [OperationContract]
    public string FDT_DK_LICH_HOC_LKE(object nam_id, object hk_id)
    {
        try
        {
            DataTable b_dt = dm_dt.Fdt_DK_HOC_LKE(LibConvert.ObjectToDouble(nam_id), LibConvert.ObjectToString(hk_id));
            return LibTable.TableToJson(b_dt);
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

    //Đăng ký học
    //[OperationContract]
    //public string FDT_TKB_DKY_HOC(string nam, string lop, string hk)
    //{
    //    try
    //    {
    //        dm_dt.FDT_TKB_DKY_HOC(LibConvert.ObjectToDouble(nam), LibConvert.ObjectToDouble(lop), hk);
    //        return "";
    //    }
    //    catch (Exception e)
    //    {
    //        return LibForm.GetError(e.Message);
    //    }
    //}

    [OperationContract]
    public string FDT_TKB_DKY_HOC(object nam, object hoc_ky, object[] lop, object[] ten_lop, object[] b_chon)
    {
        try
        {
            DataTable b_dt = new DataTable();
            Lib.Helper.SessionHelper _nsd = new Lib.Helper.SessionHelper();
            LibTable.AddNewColumns(ref b_dt, new string[] { "nsd", "nam", "lop", "ten_lop", "hoc_ky", "chon" });
            for (int i = 0; i < lop.Length; i++)
            {
                DataRow _row = b_dt.NewRow();
                _row["nsd"] = _nsd.nsd;
                _row["nam"] = nam;
                _row["lop"] = lop[i];
                _row["ten_lop"] = ten_lop[i];
                _row["hoc_ky"] = hoc_ky;
                _row["chon"] = b_chon[i];
                b_dt.Rows.Add(_row);
            }

            //nhaapj
            Lib.Data.DataSql.ExeCuteProcedure(b_dt, "PDT_TKB_DANG_KY_HOC");
            return "";
        }
        catch (Exception e)
        {
            return LibForm.GetError(e.Message);
        }
    }

}
